package com.xcity.robot.net.test;

import java.io.File;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xcity.robot.net.NetIoHandler;
import com.xcity.robot.net.pomelo.PomeloDecoder;
import com.xcity.robot.net.pomelo.PomeloEncoder;
import com.xcity.robot.net.util.Settings;
import com.xcity.robot.net.websocket.WebsocketDecoder;
import com.xcity.robot.net.websocket.WebsocketEncoder;

import naga.x.common.Config;
import naga.x.util.IOUtils;
import naga.x.util.IOUtils.LineReaderListener;

public class BatchCreateRobotsMain {

	static List<String> names = new ArrayList<>(1024);
	static List<String> icons = new ArrayList<>(1024);
	
	public static void main(String[] args) {
		JSON.DEFAULT_GENERATE_FEATURE = SerializerFeature.config(JSON.DEFAULT_GENERATE_FEATURE, SerializerFeature.WriteEnumUsingToString, false);
		JSON.DEFAULT_GENERATE_FEATURE = SerializerFeature.config(JSON.DEFAULT_GENERATE_FEATURE, SerializerFeature.WriteEnumUsingName, false);
		
		// avoid null pointer exception in WebSocketDecoder
		naga.x.App.getApp().setConfig(new Config());
//		naga.x.App.getApp().getConfig().put(Config.WEBSOCKET_REMOTE_ADDR, "121.41.120.228");
		naga.x.App.getApp().getConfig().remoteAddr = "121.41.120.228";
//		naga.x.App.getApp().getConfig().put(Config.WEBSOCKET_PORT, 7010);
		naga.x.App.getApp().getConfig().port = 7010;
		
		load();
		Settings.pomeloHandshakeListener = new Resources.PomeloHandshakeListener();
	
		final SocketConnector conn = new NioSocketConnector();
		conn.getFilterChain().addLast("codec.websocket", new ProtocolCodecFilter(new WebsocketEncoder(), new WebsocketDecoder()));
		conn.getFilterChain().addLast("codec.pomelo", new ProtocolCodecFilter(new PomeloEncoder(), new PomeloDecoder()));
		conn.setHandler(new NetIoHandler());
//		ConnectFuture cf = conn.connect(new InetSocketAddress("121.41.120.228", 7010));
		ConnectFuture cf = conn.connect(new InetSocketAddress("127.0.0.1", 3010));
	}
	
	static void load() {
		IOUtils.read(new File("C:/Users/yang.li/Desktop/server_release/robot_names/names.txt"), IOUtils.UTF8, new LineReaderListener() {
			@Override
			public boolean readLine(String line) {
				if ((line = line.trim()).length() > 0) {
					names.add(line);
				}
				return true;
			}
		});
		IOUtils.read(new File("C:/Users/yang.li/Desktop/server_release/robot_names/icons.txt"), IOUtils.UTF8, new LineReaderListener() {
			@Override
			public boolean readLine(String line) {
				if ((line = line.trim()).length() > 0) {
					icons.add(line);
				}
				return true;
			}
		});
	}
	
	static Random rnd = new Random();
	public static String nextName() {
		synchronized (names) {
			return names.isEmpty() ? null : names.remove(rnd.nextInt(names.size()));
		}
	}
	
	public static String nextIcon() {
		synchronized (icons) {
			return icons.isEmpty() ? null : icons.remove(rnd.nextInt(icons.size()));
		}
	}
	
	public static synchronized boolean hasNameAndIcon() {
		return names.size() > 0 && icons.size() > 0;
	}
	
}
